// constSpringDisSerial.cpp: implementation of the ConstSpringDisSerial class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Phsshare.h"
#include "constSpringDisSerial.h"
#include "DBConnectionManager.h"
#include "ProductCatalogManager.h"
#include "StandardOfConstSpring.h"
#include "PersistentFacade.h"
#include "StandardGenerator.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace JointSystem;
using namespace DAWrapper;

ConstSpringDisSerial::ConstSpringDisSerial()
{
	m_nIndex = -1;
}

ConstSpringDisSerial::~ConstSpringDisSerial()
{

}

BOOL ConstSpringDisSerial::GetDisSerial()
{
	m_theDisSerial.RemoveAll();

	CString strSerialTable = GetDisSerialTable();

	CString strSql;
	strSql.Format(_T("select * from %s order by DisplacementSerial"), strSerialTable);

	_RecordsetPtr rs;
	try
	{
		_ConnectionPtr* pCon = DBConnectionManager::Instance()->GetConnectionPtrByUri(this->GetSourceURL());
		ASSERT(pCon);

		rs = (*pCon)->Execute(_bstr_t(strSql), NULL, adCmdText);
		ASSERT(rs != NULL);
		if(rs == NULL)
			return FALSE;
		while(!rs->adoEOF)
		{
			m_theDisSerial.Add(vtof(rs->GetCollect(_T("DisplacementSerial"))));
			rs->MoveNext();
		}
		rs->Close();
	}
	CATCH_COM_ERROR(rs, FALSE)

	return TRUE;
}

const CString& ConstSpringDisSerial::GetDisSerialTable()
{
	StandardOfConstSpring* pSpring = this->GetStandardOfConstSpring();
	ASSERT(pSpring);
	return pSpring->GetDisplacementSerierTable();
}

DOUBLE ConstSpringDisSerial::GetFirstDis(DOUBLE dbDis)
{
	INT nMin = 0;
	INT nMax = m_theDisSerial.GetSize() - 1;
	INT nIndex = 0;

	if(nMax == 0 || dbDis < m_theDisSerial.GetAt(nMin) || 
		dbDis > m_theDisSerial.GetAt(nMax))
		return 0.0;

	for(int i = 0; i < m_theDisSerial.GetSize(); i++)
	{
		if(m_theDisSerial.GetAt(i) >= dbDis)
		{
			m_nIndex = i + 1;
			return m_theDisSerial.GetAt(i);
		}
	}
	return 0.0;
}

DOUBLE ConstSpringDisSerial::GetNextDis()
{
	if(m_nIndex >= m_theDisSerial.GetSize())
		return 0.0;

	return m_theDisSerial.GetAt(m_nIndex++);
}

BOOL ConstSpringDisSerial::InitProperty(ModelObject& object)
{
	ASSERT(FALSE);
	return TRUE;
}